package trd.smallsql;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;

public class UseCase {

	/**
	 * @param args
	 * @throws ClassNotFoundException 
	 * @throws SQLException 
	 */
	public static void main(String[] args) throws ClassNotFoundException, SQLException {
		createDatabase("D:\\test");
		createTable("D:\\test");
//		ResultSet rs=st.executeQuery("select count(*) from test");
//		while(rs.next())
//			System.out.println(rs.getInt(1));
	}

	public static Connection getConnection(String uri){
		try {
			Class.forName("smallsql.database.SSDriver");
			return DriverManager.getConnection(uri);
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}
	}
	
	public static void createDatabase(String db){
		Connection conn=getConnection("jdbc:smallsql");
		try {
			conn.createStatement().execute("create database "+db);
		} catch (SQLException e) {
			System.out.println("create SmallSQL database error:"+e.getLocalizedMessage());
		}
	}
	
	public static void createTable(String db){
		Connection conn=getConnection("jdbc:smallsql:"+db);
		String sql = "create table log"
		+ " (id int IDENTITY," +
				"clientIdentity VARCHAR(500) null," +
				"event text not null," +
				"time TIMESTAMP not null," +
				"rank VARCHAR(20) not null)";
		try {
			conn.createStatement().execute(sql);
		} catch (SQLException e) {
			System.out.println("create SmallSQL table error:"+e.getLocalizedMessage());
		}
	}
}
